package kr.co.icebird.spring3.common.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import kr.co.icebird.spring3.common.model.Menu;
import kr.co.icebird.spring3.common.service.MenuService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class CommonController {
	
	private static String htmlT = "<html>" +
			"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">" +
			"<link type=\"text/css\" rel=\"stylesheet\" href=\"/css/common.css\" />" +
			"<link type=\"text/css\" rel=\"stylesheet\" href=\"/css/style.css\" />" +
			"<link type=\"text/css\" rel=\"stylesheet\" href=\"/css/admin.css\" />";
	
	private static String htmlB = "</html>";

	@Autowired
	private MenuService menuService;
	
	@RequestMapping("/main.do")
	public void main(HttpServletRequest request,
			HttpServletResponse response) throws IOException, ServletException {
		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();		  
		out.print(htmlT);
		out.print("<title>KSWOO_SPRING</title></head>");
		out.print("<frameset rows=\"100,*\" border=\"0\" framespacing=\"0\" frameborder=\"no\">");
		out.print("<frame id=\"topFrame\" src=\"/top.do\" noresize=\"noresize\">");
		out.print("<frameset cols=\"200, *\" border=\"0\" framespacing=\"0\" frameborder=\"no\">");
		out.print("<frame id=\"leftFrame\" src=\"/menu.do\">");
		out.print("<frame id=\"bodyFrame\">");
		out.print("</frameset>");
		out.print(htmlB);
		out.close();
	}

	@RequestMapping("/menu.do")
	public void sideMenu(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();		  
		out.print(htmlT);
		out.print("<div><ul>");
		
		List<Menu> menuList =  menuService.getMenuList();

		Integer pre = null;
		for (Menu menu : menuList) {
//			if(pre == menu.getMenuUp() && pre != null){
//				out.print("<ul>");		
//			}
//			
//			if(menu.getMenuUp() == null){
//				pre = menu.getMenuId();
//				//out.print("</ul>");
//			}
			out.print("<li><a href=\""+request.getContextPath()+menu.getMenuUrl()+"\" target=\"bodyFrame\">"+menu.getMenuNm()+"</a></li>");
		}
		out.print("</ul></div>");
		out.print(htmlB);
		out.close();
	}
	
	@RequestMapping("/top.do")
	public void top(HttpServletRequest request,
			HttpServletResponse response) throws IOException, ServletException {
		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();		  
		out.print(htmlT);
		out.print("<head><title>TOP</title></head><body align=\"center\">상단입니다.</body>");
		out.print(htmlB);
		out.close();
	}
}
